#include <iostream>
#include <cassert>
#include <vector>
#include <algorithm>
#include <sstream>
#include <list>
#include <stack>
#include <queue>
#include <map>
using namespace std;

struct ListNode {
     int val;
     ListNode *next;
     ListNode() : val(0), next(nullptr) {}
     ListNode(int x) : val(x), next(nullptr) {}
     ListNode(int x, ListNode *next) : val(x), next(next) {}
};
//打印链表
void displayList(ListNode *p){
    while( p ){
        cout<< p->val ;
        p = p->next;
        if(p) cout<<"->";
    }
    cout<<endl;
}

//解决方案类
class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        ListNode *p = new ListNode(0),*rt = p;
        p->next = head;
        while(p->next){
           if( p->next->val == val ) p->next = p->next->next;
           else p = p->next;
        }
        return rt->next;
    }
};
int main(){
    Solution sp;
    ListNode *l = new ListNode(1,new ListNode(2,new ListNode(6,new ListNode(3,new ListNode(4,new ListNode(6)))))) ,*rt;
    displayList(l);
    rt = sp.removeElements(l,6);
    displayList(rt);
    return 0;
}